'''
Created on Oct 22, 2012

@author: Artur
'''
from subprocess import call
from time import gmtime, strftime

# ./pceps schema clean dirty #tuples #samples blockwise skip-eval [file-out]

exp_dir = 'C:\\Dropbox\\Research\\Thesis\\Report2\\Experiments\\'
data_dir = 'C:\\workspace\\data\\uis\\'

tempfile = exp_dir + 'temp'

nsamples = 15

sizes = ['1000', '2000', '3000', '5000', '10000', '20000', '30000', '40000', '50000']
errors = ['0', '5', '10', '15', '20', '25', '30']


def PcepsHolistic(cmd):
    cmd[6] = 'false'
    return cmd

def PcepsBlockwise(cmd):
    cmd[6] = 'true'
    return cmd

def ScaleSetSize(cmd, size):
    cmd[2] = data_dir + 'data_{}_clean'.format(size)
    cmd[3] = data_dir + 'data_{}_dirty'.format(size)
    cmd[4] = size
    return cmd

def ResultsHolistic():
    time_sum = 0.0
    with open(tempfile, 'r') as f:
        for line in f:
            time_sum += float(line)
    return time_sum / nsamples

def ResultsBlockwise():
    time_sum = 0.0
    with open(tempfile, 'r') as f:
        block_time = float(f.readline())
        for line in f:
            time_sum += float(line)
    return (block_time, time_sum / nsamples)


algs = {'PCEPS': {'holistic' : PcepsHolistic, 'blockwise' : PcepsBlockwise}}

cmds = {'PCEPS' : ['C:\\MinGW\\bin\\pceps.exe', data_dir + 'schema\\schema_P',
       '','','',str(nsamples), '', 'true', tempfile]}

# Choose which algs and options to run
# run = {'PCEPS' : ['holistic', 'blockwise']}
run = {'PCEPS' : ['blockwise']}

for alg in run.keys():
    
    cmd = cmds[alg]
    
    for opt in run[alg]:
        
        cmd = algs[alg][opt](cmd)
        
        with open(exp_dir + 'scale_{}_{}.csv'.format(alg, opt), 'w') as f:
            
            for size in sizes:

                cmd = ScaleSetSize(cmd, size)
            
                call(' '.join(cmd))

                if(opt == 'holistic'):
                    avg_time = ResultsHolistic()
                    f.write('{},{:.3f}\n'.format(size, avg_time))
                    print('done {} {} size {} in {:.3f}({:.3f}) at {}'
                            .format(alg, opt, size, avg_time * nsamples, avg_time,
                                    strftime("%H:%M:%S", gmtime())))
                else:
                    (block_time, avg_time) = ResultsBlockwise()
                    f.write('{},{:.3f},{:.3f}\n'.format(size, block_time, avg_time))
                    print('done {} {} size {} in {:.3f}, {:.3f}({:.3f}) at {}'
                            .format(alg, opt, size, block_time, avg_time * nsamples, avg_time,
                                    strftime("%H:%M:%S", gmtime())))
    

                
    